/*    */ package client.anticheat;
/*    */ 
/*    */ import com.alibaba.druid.pool.DruidPooledConnection;
/*    */ import database.DatabaseConnection;
/*    */ import java.sql.PreparedStatement;
/*    */ import java.sql.ResultSet;
/*    */ import java.sql.SQLException;
/*    */ import java.sql.Timestamp;
/*    */ import java.util.LinkedHashSet;
/*    */ import java.util.Set;
/*    */ import java.util.concurrent.locks.Lock;
/*    */ import java.util.concurrent.locks.ReentrantLock;
/*    */ import server.Timer;
/*    */ 
/*    */ 
/*    */ public class CheatingOffensePersister
/*    */ {
/* 18 */   private static final CheatingOffensePersister instance = new CheatingOffensePersister();
/* 19 */   private final Set<CheatingOffenseEntry> toPersist = new LinkedHashSet<>();
/* 20 */   private final Lock mutex = new ReentrantLock();
/*    */   
/*    */   private CheatingOffensePersister() {
/* 23 */     Timer.CheatTimer.getInstance().register(new PersistingTask(), 61000L);
/*    */   }
/*    */   
/*    */   public static CheatingOffensePersister getInstance() {
/* 27 */     return instance;
/*    */   }
/*    */   
/*    */   public void persistEntry(CheatingOffenseEntry coe) {
/* 31 */     this.mutex.lock();
/*    */     try {
/* 33 */       this.toPersist.remove(coe);
/* 34 */       this.toPersist.add(coe);
/*    */     } finally {
/* 36 */       this.mutex.unlock();
/*    */     } 
/*    */   }
/*    */ 
/*    */   
/*    */   public class PersistingTask
/*    */     implements Runnable
/*    */   {
/*    */     public void run() {
/*    */       CheatingOffenseEntry[] offenses;
/* 46 */       CheatingOffensePersister.this.mutex.lock();
/*    */       try {
/* 48 */         offenses = (CheatingOffenseEntry[])CheatingOffensePersister.this.toPersist.toArray((Object[])new CheatingOffenseEntry[CheatingOffensePersister.this.toPersist.size()]);
/* 49 */         CheatingOffensePersister.this.toPersist.clear();
/*    */       } finally {
/* 51 */         CheatingOffensePersister.this.mutex.unlock();
/*    */       } 
/*    */       
/* 54 */       try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 55 */         PreparedStatement insertps = con.prepareStatement("INSERT INTO cheatlog (characterid, offense, count, lastoffensetime, param) VALUES (?, ?, ?, ?, ?)", 1);
/* 56 */         PreparedStatement updateps = con.prepareStatement("UPDATE cheatlog SET count = ?, lastoffensetime = ?, param = ? WHERE id = ?");
/* 57 */         for (CheatingOffenseEntry offense : offenses) {
/* 58 */           String parm = (offense.getParam() == null) ? "" : offense.getParam();
/* 59 */           if (offense.getDbId() == -1) {
/* 60 */             insertps.setInt(1, offense.getChrfor());
/* 61 */             insertps.setString(2, offense.getOffense().name());
/* 62 */             insertps.setInt(3, offense.getCount());
/* 63 */             insertps.setTimestamp(4, new Timestamp(offense.getLastOffenseTime()));
/* 64 */             insertps.setString(5, parm);
/* 65 */             insertps.executeUpdate();
/* 66 */             ResultSet rs = insertps.getGeneratedKeys();
/* 67 */             if (rs.next()) {
/* 68 */               offense.setDbId(rs.getInt(1));
/*    */             }
/* 70 */             rs.close();
/*    */           } else {
/* 72 */             updateps.setInt(1, offense.getCount());
/* 73 */             updateps.setTimestamp(2, new Timestamp(offense.getLastOffenseTime()));
/* 74 */             updateps.setString(3, parm);
/* 75 */             updateps.setInt(4, offense.getDbId());
/* 76 */             updateps.executeUpdate();
/*    */           } 
/*    */         } 
/* 79 */         insertps.close();
/* 80 */         updateps.close();
/* 81 */       } catch (SQLException e) {
/* 82 */         System.err.println("error persisting cheatlog" + e);
/*    */       } 
/*    */     }
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\client\anticheat\CheatingOffensePersister.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */